package fr.vvins.core.common.dto.xml;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapAdapter<T> extends XmlAdapter<MapElements<String,T>[], Map<String, T>>
{
  @Override
public MapElements<String,T>[] marshal(Map<String, T> arg0) throws Exception
  {
    MapElements<String,T>[] mapElements = (MapElements<String, T>[]) Array.newInstance(MapElements.class, arg0.size());
 
    int i = 0;
    for (Map.Entry<String, T> entry : arg0.entrySet())
      mapElements[i++] = new MapElements<String,T>(entry.getKey(), entry.getValue());
 
    return mapElements;
  }
 
  @Override
public Map<String, T> unmarshal(MapElements<String,T>[] arg0) throws Exception
  {
    Map<String,T> r = new HashMap<String,T>();
    for(MapElements<String,T> mapelement : arg0)
      r.put(mapelement.key, mapelement.value);
    return r;
  }
}
